package com.db.MessageBox;

import java.util.ArrayList;
import java.util.List;

import com.db.R;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {

	private TextView countryName;
	private ImageView compavatar;
	private ImageView useravatar;
	private List<OneComment> countries = new ArrayList<OneComment>();
	private LinearLayout wrapper;
	public int currentUserAvatarImageResource;

	@Override
	public void add(OneComment object){
		countries.add(object);
		super.add(object);
	}
	
	public void remove(OneComment object){
		countries.remove(object);
		super.remove(object);
	}
	
	public void removeAll(){
		countries.clear();
		super.clear();
	}

	public DiscussArrayAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		currentUserAvatarImageResource = R.drawable.user_avatar;
	}

	public int getCount() {
		return this.countries.size();
	}

	public OneComment getItem(int index) {
		return this.countries.get(index);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = null;
		if (row == null) {
			LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.listitem_discuss, null, false);
		}

		wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

		OneComment coment = getItem(position);

		countryName = (TextView) row.findViewById(R.id.comment);
		compavatar = (ImageView) row.findViewById(R.id.compavatar);
		useravatar = (ImageView) row.findViewById(R.id.useravatar);
		if(coment.left){
			//comp avatar
			compavatar.setVisibility(ImageView.VISIBLE);
			useravatar.setVisibility(ImageView.GONE);
			//countryName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		}
		else{
			//user avatar
			compavatar.setVisibility(ImageView.GONE);
			useravatar.setVisibility(ImageView.VISIBLE);
			//countryName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT-((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 64, DisplayMetrics.DENSITY_DEFAULT)), LayoutParams.WRAP_CONTENT));
		}
		//Log.d(countries.size()+" ORIG IMAGAE ID: ", ""+useravatar.getId());
		/*
		if(countries.size()%3==0){
			useravatar.setImageResource(R.drawable.user_avatar);
		}
		else
			useravatar.setImageResource(R.drawable.user_avatar2);
			
		*/
		if(currentUserAvatarImageResource!=0){
			useravatar.setImageResource(currentUserAvatarImageResource);
		}
		
		countryName.setText(coment.category+" "+coment.comment);
		
		countryName.setBackgroundResource(coment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
		wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);

		return row;
	}

	public Bitmap decodeToBitmap(byte[] decodedByte) {
		return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
	}
	
	public ImageView getUserAvatarImageView(){
		return this.useravatar;
	
	}
	
	  

}